<?php
/**
 * Created by PhpStorm.
 * User: Bostin
 * Date: 2019-02-14
 * Time: 17:54
 */

namespace Bostin\Translator\Provider;


use TencentCloud\Common\Credential;
use TencentCloud\Tmt\V20180321\Models\ImageTranslateRequest;
use TencentCloud\Tmt\V20180321\Models\SpeechTranslateRequest;
use TencentCloud\Tmt\V20180321\Models\TextTranslateRequest;
use TencentCloud\Tmt\V20180321\TmtClient;

/**
 * Class TencentProvider
 * @package Bostin\Translator\Provider
 */
class TencentProvider extends AbstractProvider
{
    /**
     * @var
     */
    protected $credential;

    /**
     * @var
     */
    protected $client;

    /**
     * @return Credential
     */
    public function getCredential() {
        if (is_null($this->credential)) {
            $this->credential = new Credential($this->config['secretId'] ?? '', $this->config['secretKey'] ?? '');
        }
        return $this->credential;
    }

    /**
     * @return TmtClient
     */
    public function getClient() {
        if (is_null($this->client)) {
            $this->client = new TmtClient($this->getCredential(), $this->config['region'] ?? 'ap-shanghai-fsi');
        }
        return $this->client;
    }

    /**
     * @param string $sourceText
     * @param string $source
     * @param string $target
     * @return string|null
     */
    public function textTranslate(string $sourceText, string $source, string $target): ?string
    {
        $request = new TextTranslateRequest();
        $request->setProjectId(0);
        $request->setSourceText($sourceText);
        $request->setSource($source);
        $request->setTarget($target);

        return $this->getClient()->TextTranslate($request)->getTargetText() ?? null;
    }

    /**
     * @param string $sourceText
     * @param string $source
     * @param string $target
     * @return string|null
     */
    public function imageTranslate(string $sourceText, string $source, string $target): ?string
    {
        $request = new ImageTranslateRequest();
        $request->setProjectId(0);
        $request->setSessionUuid(microtime(true));
        $request->setScene("doc");
        $request->setData($sourceText);
        $request->setSource($source);
        $request->setTarget($target);

        return $this->getClient()->ImageTranslate($request)->getImageRecord()->Value["TargetText"] ?? null;
    }

    /**
     * @param string $audioFormat
     * @param int $seq
     * @param int $isEnd
     * @param string $sourceText
     * @param string $source
     * @param string $target
     * @return string|null
     */
    public function speechTranslate(string $audioFormat, int $seq, int $isEnd, string $sourceText, string $source, string $target): ?string
    {
        $request = new SpeechTranslateRequest();
        $request->setProjectId(0);
        $request->setSessionUuid(microtime(true));
        $request->setSource($source);
        $request->setTarget($target);
        $request->setAudioFormat($audioFormat);
        $request->setSeq($seq);
        $request->setIsEnd($isEnd);
        $request->setData($sourceText);

        return $this->getClient()->SpeechTranslate($request)->getTargetText() ?? null;
    }
}